package com.example.codegenerator.mojo;

import com.example.codegenerator.util.ModuleStringWriterUtils;
import com.example.codegenerator.util.StringUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.*;

/**
 * 制定名称模块/目录下的代码生成
 */
@Execute(phase = LifecyclePhase.COMPILE)
@Mojo(name = "module", requiresDependencyResolution = ResolutionScope.COMPILE)
public class ModuleWithMysqlJDBCMojo extends AbstractMojo {

    //项目包路径
    @Parameter(property = "basePath", defaultValue = "${basePath}")
    private String basePath;

    //元数据文件路径
    @Parameter(property = "metaDataPath", defaultValue = "${metaDataPath}")
    private String metaDataPath;

    //是否需要覆盖
    @Parameter(property = "ifModuleOverwrite", defaultValue = "${ifModuleOverwrite}")
    private String moduleOverwrite;

    //表名称
    @Parameter(property = "tableNames", defaultValue = "${tableNames}")
    private String tableNames;

    //是否需要swagger
    @Parameter(property = "swaggerFlag", defaultValue = "${swaggerFlag}")
    private String swaggerFlag;

    @Override
    public void execute() {
        Log log = getLog();
        basePath = StringUtils.getBasePath(basePath);
        if(basePath == null) {
            log.error("basePath不能为空");
            return;
        }
        ModuleStringWriterUtils.mkdir(basePath, log);
        ModuleStringWriterUtils.generateCommonFiles(basePath);
        ModuleStringWriterUtils.generateModuleFiles(metaDataPath, basePath, tableNames,
                swaggerFlag, moduleOverwrite);
    }
}
